package player;

import java.awt.Dimension;
import java.awt.EventQueue;  
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.io.File;  
  
















import javax.swing.JFileChooser;  
import javax.swing.JFrame;
import javax.swing.SwingWorker;  
  
















import com.sun.jna.Native;  
import com.sun.jna.NativeLibrary;  
  
















import uk.co.caprica.vlcj.binding.LibVlc;  
import uk.co.caprica.vlcj.runtime.RuntimeUtil;  
  
public class PlayerMain {  
  
    static Window frame; 
    private static int playPercent = 0;
    //private static final String NATIVE_LIBRARY_SEARCH_PATH = "D:\\Program Files\\VideoLAN\\VLC\\sdk\\lib";  
  
    public static void main(String[] args) {
    	
    	String videoPath = "H:\\04心情舒缓.mp4";
    	start(videoPath);  
    }

	private static void start(final String videoPath) {
		String sysPath = System.getProperty("user.dir");
		System.out.println(sysPath);
        //环境配置，将vlc sdk导入到eclipse  
          
        //if(RuntimeUtil.isWindows()){                  }  
        NativeLibrary.addSearchPath(  
                RuntimeUtil.getLibVlcLibraryName(), sysPath+"\\sdk\\lib");  //导入的路径是vlc的安装路径  
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),LibVlc.class);  
        //System.out.println(LibVlc.INSTANCE.libvlc_get_version());  
  
        //创建主程序界面运行窗体  
        EventQueue.invokeLater(new Runnable() {  
              
             
            public void run() {  
                // TODO Auto-generated method stub  
                try{  
                    frame=new Window(); 
                    frame.setUndecorated(true); 
                    frame.setVisible(true); 
                    frame.getMediaPlayer().playMedia(videoPath);  //直接播放视屏，参数是视屏文件的绝对路径  
                    playPercent = 0;
//                    frame.getMediaPlayer().prepareMedia("E:\\bb.mp4");       //控制播放视屏  
                    new SwingWorker<String, Integer>() {  
  
                         
                        protected String doInBackground() throws Exception {  
                            // TODO Auto-generated method stub  
                            while (true) {  //获取视频播放进度并且按百分比显示  
                            	if(playPercent == 100){
                            		frame.getMediaPlayer().playMedia(videoPath);  //直接播放视屏，参数是视屏文件的绝对路径  
                            	}
                                long total=frame.getMediaPlayer().getLength();  
                                long curr=frame.getMediaPlayer().getTime();  
                                float percent=(float)curr/total;  
                                playPercent = (int)(percent*100);
                                Thread.sleep(100);  
                            }     
                            //return null;  
                        }  
                          
                        protected void process(java.util.List<Integer> chunks) { 
                        	
                        }  
                    }.execute();  
                }catch(Exception e){  
                    e.printStackTrace();  
                }  
            }  
        });
	}  
      
    //打开文件  
    public static void openVideo() {  
        JFileChooser chooser=new JFileChooser();  
        int v=chooser.showOpenDialog(null);  
        if(v==JFileChooser.APPROVE_OPTION){  
            File file=chooser.getSelectedFile();  
            frame.getMediaPlayer().playMedia(file.getAbsolutePath());  
        }  
    }  
      
    //退出播放  
    public static void exit() {  
        frame.getMediaPlayer().release();  
        frame.dispose();
    }  
  
    //实现播放按钮的方法  
    public static void play() {  
        frame.getMediaPlayer().play();  
    }  
      
    //实现暂停按钮的方法  
    public static void pause() {  
        frame.getMediaPlayer().pause();  
    }  
      
    //实现停止按钮的方法  
    public static void stop() {  
        frame.getMediaPlayer().stop();  
    }  
      
    //实现点击进度条跳转的方法  
    public static void jumpTo(float to) {  
        frame.getMediaPlayer().setTime((long)(to*frame.getMediaPlayer().getLength()));  
    }  
      
    //实现控制声音的方法  
    public static void setVol(int v) {  
        frame.getMediaPlayer().setVolume(v);  
    }  
      
}  